home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DJGPP / BCCGRX12.ZIP / contrib / bcc2grx / src / bccgrx09.c < prev    next >
C/C++ Source or Header  |  1993-05-22  |  3KB  |  131 lines

  1. /*
  2.  *  BCC2GRX  -  Interfacing Borland based graphics programs to LIBGRX
  3.  *  Copyright (C) 1993  Hartmut Schirmer
  4.  *
  5.  *  see bccgrx.c for details
  6.  */
  7.  
  8. #include "bccgrx00.h"
  9.  
  10. static GrContext *draw;
  11. static int lx, ly, mx, my;
  12. static int _border;
  13.  
  14. #define UP   0x01
  15. #define DOWN 0x02
  16.  
  17. static int _floodfill(int x, int y, int flg, int xl, int xr)
  18. {
  19.   int sx, xx;
  20.  
  21.   sx = x;
  22.   while ( sx > lx && GrPixelNC(sx-1,y) != _border)
  23.     --sx;
  24.   while ( x < mx && GrPixelNC(x+1,y) != _border)
  25.     ++x;
  26.   if (draw == NULL)
  27.     GrHLine(sx, x, y, _border); /* _border == Fill color ! */
  28.   else {
  29.     GrSetContext( NULL);
  30.     switch (FPATT) {
  31.       case SOLID_FILL : GrHLine( sx, x, y, FILL);
  32.             break;
  33.       case EMPTY_FILL : GrHLine( sx, x, y, COLBG);
  34.             break;
  35.       default         : GrPatternFilledLine( sx, y, x, y, &FILLP);
  36.             break;
  37.     }
  38.     GrSetContext(draw);
  39.     GrHLineNC(sx, x, y, _border);
  40.   }
  41.   switch (flg) {
  42.     case UP  : if (y<my) {
  43.          ++y;
  44.          for (xx=sx; xx <= x; ++xx)
  45.            if (GrPixelNC(xx,y) != _border)
  46.              xx = _floodfill(xx,y,UP,sx,x);
  47.          --y;
  48.            }
  49.            if (y>ly) {
  50.          --y;
  51.          for (xx=sx; xx <= xl; ++xx)
  52.            if (GrPixelNC(xx,y) != _border)
  53.              xx = _floodfill(xx,y,DOWN,sx,x);
  54.          for (xx=xr; xx <= x; ++xx)
  55.            if (GrPixelNC(xx,y) != _border)
  56.              xx = _floodfill(xx,y,DOWN,sx,x);
  57.            }
  58.            break;
  59.     case DOWN: if (y>ly) {
  60.          --y;
  61.          for (xx=sx; xx <= x; ++xx)
  62.            if (GrPixelNC(xx,y) != _border)
  63.              xx = _floodfill(xx,y,DOWN,sx,x);
  64.          ++y;
  65.            }
  66.            if (y<my) {
  67.          ++y;
  68.          for (xx=sx; xx <= xl; ++xx)
  69.            if (GrPixelNC(xx,y) != _border)
  70.              xx = _floodfill(xx,y,UP,sx,x);
  71.          for (xx=xr; xx <= x; ++xx)
  72.            if (GrPixelNC(xx,y) != _border)
  73.              xx = _floodfill(xx,y,UP,sx,x);
  74.            }
  75.            break;
  76.     default  : if (y>ly) {
  77.          --y;
  78.          for (xx=sx; xx <= x; ++xx)
  79.            if (GrPixelNC(xx,y) != _border)
  80.              xx = _floodfill(xx,y,DOWN,sx,x);
  81.          ++y;
  82.            }
  83.            if (y<my) {
  84.          ++y;
  85.          for (xx=sx; xx <= x; ++xx)
  86.            if (GrPixelNC(xx,y) != _border)
  87.              xx = _floodfill(xx,y,UP,sx,x);
  88.            }
  89.            break;
  90.   }
  91.   return x;
  92. }
  93.  
  94. void floodfill(int x, int y, int border)
  95. {
  96.   _DO_INIT_CHECK;
  97.  
  98.   if (__gr_clip) {
  99.     lx = VL; ly = VT;
  100.     mx = VR; my = VB;
  101.   } else {
  102.     lx = 0; ly = 0;
  103.     mx = GrScreenX() - 1;
  104.     my = GrScreenY() - 1;
  105.   }
  106.   x += VL;
  107.   y += VT;
  108.   if ( x < lx || y < ly || x > mx || y > my || GrPixel(x,y) == border)
  109.     return;
  110.  
  111.   _border = border;
  112.   if (  (border == FILL  && FPATT == SOLID_FILL)
  113.       ||(border == COLBG && FPATT == EMPTY_FILL)) {
  114.     draw = NULL;
  115.     _floodfill(x,y,0,0,0);
  116.   } else {
  117.     draw = GrCreateContext( GrScreenX(), GrScreenY(), NULL, draw);
  118.     if (draw == NULL) {
  119.       ERR = grNoFloodMem;
  120.       return;
  121.     }
  122.     GrBitBlt(draw, lx, ly, NULL, lx, ly, mx, my, GrWRITE);
  123.     GrSetContext( draw);
  124.     FILLP.gp_bmp_fgcolor = FILL;
  125.     FILLP.gp_bmp_bgcolor = COLBG;
  126.     _floodfill(x,y,0,0,0);
  127.     GrSetContext( NULL);
  128.     GrDestroyContext(draw);
  129.   }
  130. }
  131.